//
//  HWHomeProtocol.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/24.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWHomeProtocol: NSObject {
    var model:HWHomeModel?
    
    var didClickNav:((_:HWHomeNavModel)->())?
}

extension HWHomeProtocol:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            self.didClickNav?(model?.nav[indexPath.row] ?? HWHomeNavModel())
        }
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        
        //        if kind == UICollectionView.elementKindSectionHeader {
        //        if indexPath.section != 0 {
        let  header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWHomeHeader", for: indexPath) as! HWHomeHeader
        if indexPath.section == 1 {
            header.titleLabel.text = "数据播报"
            header.imageView.image = UIImage(named: "dataBroadcast")
        }else if indexPath.section == 2 {
            header.titleLabel.text = "推广排行榜"
            header.imageView.image = UIImage(named: "priceRank")
        }
        return header
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 || section == 3 || section == 4{
            return CGSize.zero
        }else {
            if section == 2 {
                guard (model?.paihangbang.count ?? -1) > 0 else {
                    return CGSize.zero
                }
            }
            return CGSize(width: screenWidth, height: 44*widthScale)
            
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if section == 2 {
            return UIEdgeInsets(top: 0, left: 22*widthScale, bottom: 0, right: 22*widthScale)
        }else {
            return UIEdgeInsets.zero
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize(width: screenWidth/4, height: 70*widthScale)
        }else if indexPath.section == 1 {
            return CGSize(width: screenWidth, height: 490*widthScale)
        }else if indexPath.section == 2 {
            return CGSize(width: (screenWidth-44*widthScale)/3, height: 162*widthScale)
        }else if indexPath.section == 3 {
            return CGSize(width: (screenWidth - 40*widthScale), height: 46*widthScale)
        }else{
            return CGSize.zero
        }
        
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return model?.nav.count ?? 0
        }else if section == 2 {
            if model?.paihangbang.count ?? 0 > 3 {
                return 3
            }else {
                return model?.paihangbang.count ?? 0
            }
            
        }else if section == 3 {
            if model?.paihangbang.count ?? 0 > 3 {
                return (model?.paihangbang.count)! - 3 + 1
            }else {
                return 0
            }
        }
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let funcCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeFunctionCell", for: indexPath) as! HWHomeFunctionCell
        let dataCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeDataCell", for: indexPath) as! HWHomeDataCell
        let topThreeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeTopThreeRankCell", for: indexPath) as! HWHomeTopThreeRankCell
        let otherThreeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeOtherRankCell", for: indexPath) as! HWHomeOtherRankCell
        if indexPath.section == 0 {
            funcCell.model = model?.nav[indexPath.row]
            return funcCell
        }else if indexPath.section == 1 {
            dataCell.model = model
            return dataCell
        }else if indexPath.section == 2 {
            topThreeCell.indexPath = indexPath
            topThreeCell.model = model?.paihangbang[indexPath.row]
            return topThreeCell
        }else {
            otherThreeCell.indexPath = indexPath
            if indexPath.row == 0 {
                
            }else {
                otherThreeCell.model = model?.paihangbang[indexPath.row + 3 - 1]
            }
            
            return otherThreeCell
        }
    }
    
    
}
